﻿using Sprout.Schedule.Consts;
using Sprout.Schedule.Dtos;
using Sprout.Schedule.Interfaces;
using Quartz;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Sprout.Core.Extensions;
using Sprout.Schedule.Enums;
using Sprout.Core;

namespace Sprout.Schedule.Jobs
{


    public abstract class JobBase
    {
        private Lazy<ITaskService> taskService = ServiceLocator.GetLazyService<ITaskService>();
        protected Lazy<ITaskLogService> taskLogService = ServiceLocator.GetLazyService<ITaskLogService>();

        private long taskId = 0;
        public long TaskId
        {
            get
            {
                if (taskId == 0)
                {
                    throw new Exception("未获取到TaskId");
                }
                return taskId;
            }
            set
            {
                taskId = value;
            }
        }

        #region 异步
        /// <summary>
        /// 开始任务
        /// </summary>
        /// <returns></returns>
        public async Task JobRunAsync()
        {

            try
            {
                await taskService.Value.RunAsync(TaskId);
            }
            catch (Exception e)
            {
                await LogAsync($"运行任务异常：{e.Message}");
            }
        }
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <returns></returns>
        public async Task JobRunEndAsync()
        {
            try
            {
                await taskService.Value.RunEndAsync(TaskId);
            }
            catch (Exception e)
            {
                await LogAsync($"结束任务异常：{e.Message}");
            }
        }



        /// <summary>
        /// 获取任务详情
        /// </summary>
        /// <returns></returns>
        public async Task<TaskInfo> GetTaskInfoAsync()
        {
            return await taskService.Value.GetDetailAsync(TaskId);
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task<T> GetParametersAsync<T>()
        {
            var task = await GetTaskInfoAsync();
            if (task.Parameters == null)
            {
                return default(T);
            }
            return task.Parameters.FromJsonString<T>();
        }

        /// <summary>
        /// 获取上次执行的值
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetLastValueAsync()
        {
            var task = await GetTaskInfoAsync();
            return task.RunValue;
        }

        /// <summary>
        /// 修改计划任务最后执行值
        /// </summary>
        /// <param name="lastValue"></param>
        public async Task UpdateRunValueAsync(string lastValue)
        {
            try
            {
                await taskService.Value.UpdateRunValueAsync(TaskId, lastValue);
            }
            catch (Exception e)
            {
                await LogAsync($"修改执行的值异常：{e.Message}");
            }
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="content">日志内容</param>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        public async Task LogAsync(string content, Exception exception = null)
        {
            await taskLogService.Value.LogAsync(TaskId, content, exception);
        }

        #endregion

        #region 同步

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="content">日志内容</param>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        public void Log(string content, Exception exception = null)
        {
            taskLogService.Value.Log(TaskId, content, exception);
        }
        /// <summary>
        /// 开始任务
        /// </summary>
        /// <returns></returns>
        public void JobRun()
        {

            try
            {
                taskService.Value.Run(TaskId);
            }
            catch (Exception e)
            {
                Log($"运行任务异常：{e.Message}");
            }
        }
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <returns></returns>
        public void JobRunEnd()
        {
            try
            {
                taskService.Value.RunEnd(TaskId);
            }
            catch (Exception e)
            {
                Log($"结束任务异常：{e.Message}");
            }
        }
        /// <summary>
        /// 获取任务详情
        /// </summary>
        /// <returns></returns>
        public TaskInfo GetTaskInfo()
        {
            return taskService.Value.GetDetail(TaskId);
        }
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetParameters<T>()
        {
            var task = GetTaskInfo();
            if (task.Parameters == null)
            {
                return default(T);
            }
            return task.Parameters.FromJsonString<T>();
        }

        /// <summary>
        /// 修改计划任务最后执行值
        /// </summary>
        /// <param name="lastValue"></param>
        public void UpdateRunValue(string lastValue)
        {
            try
            {
                taskService.Value.UpdateRunValue(TaskId, lastValue);
            }
            catch (Exception e)
            {
                Log($"修改执行的值异常：{e.Message}");
            }
        }

        /// <summary>
        /// 获取上次执行的值
        /// </summary>
        /// <returns></returns>
        public string GetLastValue()
        {
            var task = GetTaskInfo();
            return task.RunValue;
        }

        #endregion
    }
}
